package app.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import app.utils.Md5;
import siotwx.dao.UserDao;
import siotwx.entity.User;

/**
 * 这个类用于处理系统用户和人员相关的业务逻辑
 * @author 昊琦
 *创建时间：2018年3月9日12:18:46
 */

@Service("UserManageService")
public class UserManageService {
	private UserDao userDao;

	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	/**
	 * 根据用户名和密码查询用户信息
	 * @author 昊琦
	 * @param name 用户名
	 * @param password 密码
	 * @return
	 * 创建时间：2018年3月6日09:40:41
	 */
	@Transactional(readOnly = true)
	public User getItem(String name,String password){
		
		//把用户名和密码转换成MD5
		Md5 md5 = new Md5();
		String nameDigest = md5.getDigest(name);
		String passwordDigest = md5.getDigest(password);
		
		//获取用户
		Map<String, Object> params = new HashMap<>();
		params.put("nameDigest", nameDigest);
		params.put("pwdDigest", passwordDigest);
		List<User> list = userDao.getlist(params);
		
		if (list==null||list.isEmpty()) {
			return null;
		}else{
			return list.get(0);
		}
		
	}
	
	/**
	 * 根据openId获取用户信息
	 * @author 昊琦
	 * @param openId
	 * @return
	 */
	@Transactional(readOnly = true)
	public User getItemByOpenId(String openId){
		
		
		Map<String, Object> params = new HashMap<>();
		params.put("openid", openId);
		
		List<User> list = userDao.getlist(params);
		
		if (list==null||list.isEmpty()) {
			return null;
		}else{
			return list.get(0);
		}
	}
	
	/**
	 * 获取该地区的所有未被冻结工人的名单
	 * @author 昊琦
	 * @param regionId
	 * @return
	 */
	@Transactional(readOnly=true)
	public List<Map<String, Object>> getAllWorkerThisRegion(int regionId){
		
		Map<String, Object> param = new HashMap<>();
		param.put("frozen", 0);
		param.put("role_id", 3);
		param.put("region_id", regionId);
		
		return userDao.getListBesidesZero(param);
	}
	
	
	/**
	 * 根据主键id查询用户信息
	 * @author 昊琦
	 * @param id 主键id
	 * @return
	 */
	@Transactional(readOnly=true)
	public User findUser(int id){
		
		return userDao.get(id);
	}
	
}
